//
//  ZZNewFeatureViewController.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/13.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let imageCount = 4
class ZZNewFeatureViewController: UICollectionViewController {

    init(){
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        //设置分页
        collectionView?.pagingEnabled = true
        //取消滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        //取消弹簧效果
        collectionView?.bounces = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    //显示item的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell

        cell.index = indexPath.item
        
        return cell
    }
    //停止滚动的时候执行动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / screenW)
        print(page)
        
        if page == imageCount - 1 {
            
            if let cell =  collectionView?.visibleCells().last as? NewFeatureCell {
             cell.startAnimation()
            }
        }
    }
}

class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
        didSet{
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    //按钮点击事件
    @objc private func start() {
        print("KO")
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    
    //按钮动画
    private func startAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("KO")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件
    private func setUpUi() {
        
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        //背景图片约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        //开始按钮约束
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        //开始按钮的点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
    }
    
    //懒加载控件
    //背景图片
    private lazy var iconImage : UIImageView = UIImageView()
    //体验按钮
    private lazy var startButton: UIButton = UIButton(title: "立即体验", image: nil, backImage: "new_feature_finish_button", color: nil)
    
    
    
}
